package com.idiot.web.log;

import com.github.pagehelper.Page;
import com.idiot.domain.common.utils.ResponseUtils;
import com.idiot.domain.model.Log;
import com.idiot.domain.model.LogType;
import com.idiot.web.base.SuperController;
import com.idiot.service.ILogService;
import com.idiot.service.ILogTypeService;
import org.apache.ibatis.session.RowBounds;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by idiot on 2016/12/17.
 */
@Controller
@RequestMapping(value = "log")
public class LogController extends SuperController {
    private static final Logger logger = LoggerFactory.getLogger(LogController.class);

    @Resource
    private ILogService logService;
    @Resource
    private ILogTypeService logTypeService;

    //加载page页面
    @RequiresPermissions(value = "log:page")
    @RequestMapping(value = "page", method = RequestMethod.GET)
    public String toPage(Model model){
        //加载日志类型
        List<LogType> list = logTypeService.getAllValidLogTypes();
        model.addAttribute("logTypes", list);
        return "log/page";
    }

    //加载data页面
    @RequestMapping(value = "data", method = RequestMethod.POST)
    public String data(Integer pageNo, @RequestParam(defaultValue="10")Integer pageSize, Log info, Model model){
        Page<Log> lists = logService.getAllLogs(new RowBounds((pageNo-1)*pageSize, pageSize));
        model.addAttribute("lists", lists);
        return "log/data";
    }

    //删除菜单信息
    @RequestMapping(value = "{logId}/delete", method = RequestMethod.POST)
    public void delete(@PathVariable("logId") long logId){
        logService.delete(logId);
        ResponseUtils.writeSuccessReponse(request,response,"删除日志信息成功");
    }
}
